package org.jeecg.modules.procurement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.BeanPlusUtil;
import org.jeecg.modules.procurement.entity.BussMaterialProcurement;
import org.jeecg.modules.procurement.entity.BussMaterialProcurementDetail;
import org.jeecg.modules.procurement.mapper.BussMaterialProcurementDetailMapper;
import org.jeecg.modules.procurement.mapper.BussMaterialProcurementMapper;
import org.jeecg.modules.procurement.service.IBussMaterialProcurementService;
import org.jeecg.modules.procurement.vo.BussMaterialProcurementDetailVo;
import org.jeecg.modules.procurement.vo.BussMaterialProcurementPage;
import org.jeecg.modules.procurement.vo.BussMaterialProcurementPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 原料采购订单
 * @Author: jeecg-boot
 * @Date:   2024-06-06
 * @Version: V1.0
 */
@Service
public class BussMaterialProcurementServiceImpl extends ServiceImpl<BussMaterialProcurementMapper, BussMaterialProcurement> implements IBussMaterialProcurementService {

	@Autowired
	private BussMaterialProcurementMapper bussMaterialProcurementMapper;
	@Autowired
	private BussMaterialProcurementDetailMapper bussMaterialProcurementDetailMapper;

	@Override
	public IPage<BussMaterialProcurementPage> pagelist(Page<BussMaterialProcurementPage> page, QueryWrapper<BussMaterialProcurementPage> queryWrapper) {

		return bussMaterialProcurementMapper.pagelist(page, queryWrapper);
	}

	/**
	 *
	 * @param page
	 * @param queryWrapper
	 * @return
	 */
	@Override
	public IPage<BussMaterialProcurementPage> procurementPage(Page<BussMaterialProcurementPage> page, QueryWrapper<BussMaterialProcurementPage> queryWrapper) {
		return bussMaterialProcurementMapper.procurementPage(page,queryWrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(BussMaterialProcurement bussMaterialProcurement, List<BussMaterialProcurementDetailVo> details) {
		bussMaterialProcurementMapper.insert(bussMaterialProcurement);
		if(details!=null && details.size()>0) {
			for(BussMaterialProcurementDetailVo entity:details) {
				BussMaterialProcurementDetail detail = BeanPlusUtil.toBean(entity, BussMaterialProcurementDetail.class);
				//外键设置
				detail.setProcurementId(bussMaterialProcurement.getId());
				detail.setProcurementCode(bussMaterialProcurement.getCode());
				detail.setExt1(bussMaterialProcurement.getCode()+"-"+entity.getMaterialCode()+"-"+entity.getSupplierCode());
				bussMaterialProcurementDetailMapper.insert(detail);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(BussMaterialProcurement bussMaterialProcurement,List<BussMaterialProcurementDetailVo> details) {
		bussMaterialProcurementMapper.updateById(bussMaterialProcurement);
		
		//1.先删除子表数据
		bussMaterialProcurementDetailMapper.deleteByMainId(bussMaterialProcurement.getId());
		
		//2.子表数据重新插入
		if(details!=null && details.size()>0) {
			for(BussMaterialProcurementDetailVo detail:details) {
				BussMaterialProcurementDetail entity = BeanPlusUtil.toBean(detail, BussMaterialProcurementDetail.class);
				//外键设置
				entity.setProcurementId(bussMaterialProcurement.getId());
				entity.setProcurementCode(bussMaterialProcurement.getCode());
				entity.setExt1(bussMaterialProcurement.getCode()+"-"+detail.getMaterialCode()+"-"+detail.getSupplierCode());
				bussMaterialProcurementDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
//		bussMaterialProcurementDetailMapper.deleteByMainId(id);
//		bussMaterialProcurementMapper.deleteById(id);
		List<BussMaterialProcurementDetail> list = bussMaterialProcurementDetailMapper.selectList(new LambdaQueryWrapper<BussMaterialProcurementDetail>()
				.eq(BussMaterialProcurementDetail::getProcurementId, id).select(BussMaterialProcurementDetail::getId));
		List<String> strlist = list.stream().map(BussMaterialProcurementDetail::getId).collect(Collectors.toList());
		this.delBatchMain(strlist);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			this.deleteData((String) id);
		}
	}

	private void deleteData(String id){
		BussMaterialProcurementDetail bussMaterialProcurementDetail = bussMaterialProcurementDetailMapper.selectById(id);
		if(bussMaterialProcurementDetail.getArrivalNum().compareTo(BigDecimal.ZERO) > 0){
			throw new RuntimeException("订单已入库，无法删除");
		}
		bussMaterialProcurementDetailMapper.deleteById(id);
		Long detailCount = bussMaterialProcurementDetailMapper.selectCount(new LambdaQueryWrapper<BussMaterialProcurementDetail>()
				.eq(BussMaterialProcurementDetail::getProcurementId, bussMaterialProcurementDetail.getProcurementId()));
		if(detailCount == 0){
			bussMaterialProcurementMapper.deleteById(bussMaterialProcurementDetail.getProcurementId());
		}
	}


	@Override
	public IPage<BussMaterialProcurementPageInfo> arrayInfo(Page<BussMaterialProcurementPageInfo> page, QueryWrapper<BussMaterialProcurementPageInfo> queryWrapper) {
		return bussMaterialProcurementMapper.arrayInfo(page,queryWrapper);
	}
}
